<?php
require_once dirname(__FILE__) . '/basic_router.class.php';

class LINKS_Router extends Basic_Router
{
	var $mydirname = NULL;
	var $get_query = FALSE;

	/**
	 * Constructor
	 * @param	string	$mydirname
	 */
	function LINKS_Router($mydirname = NULL)
	{
		if (empty($mydirname)) {
			return FALSE;
		}
		if (preg_match('/[^0-9a-zA-Z_-]/', $mydirname)) {
			return FALSE;
		}
		$this->mydirname = $mydirname;
		$this->_set_module_config();
		$this->_exception_handling();
		$this->_set_routing();
	}

	function _exception_handling()
	{
		if ($this->module_config['url_approach'] > 0 
			&& @$_GET['m'] == 'post_j' 
			&& in_array(@$_GET['c'], array('broken', 'request', 'rating'))) 
		{
			$this->module_config['url_approach'] = 0;// temporary
		}
	}

	function _add_CM_in_query()
	{
		if (isset($_GET['lid']) && !empty($_GET['lid']) && !isset($_GET['m'])) {
			$_GET['m'] = 'item';
		}
		if (isset($_GET['uid']) && !empty($_GET['uid']) && !isset($_GET['m'])) {
			$_GET['m'] = 'user';
		}
		if (isset($_GET['cid']) && !empty($_GET['cid']) && !isset($_GET['lid']) && !isset($_GET['c'])) {
			$_GET['c'] = 'category';
		}
	}

	function _add_CM_in_segment(&$segments)
	{
		if (isset($segments[0]) && ($segments[0] == 'items' || $segments[0] == 'item' || $segments[0] == 'user')) {
			array_unshift($segments, 'index');
		}
		if (isset($segments[0]) && $segments[0] == 'category' && 
			isset($segments[1]) && $segments[1] !== 'index' && $segments[1] !== 'tree') 
		{
			array_splice($segments, 1, 0, 'index');
		}
		if (isset($segments[0]) && $segments[0] == 'frame_header' && isset($segments[1]) && $segments[1] !== 'index') {
			array_splice($segments, 1, 0, 'index');
		}
		if (isset($segments[0]) && ($segments[0] == 'broken' || $segments[0] == 'rating' || $segments[0] == 'request') && 
			isset($segments[1]) && $segments[1] !== 'post' && $segments[1] !== 'form' && $segments[1] !== 'index' && $segments[1] !== 'post_j') 
		{
			array_splice($segments, 1, 0, 'form');
		}
		if (isset($segments[0]) && $segments[0] == 'visit' && 
			isset($segments[1]) && 
				$segments[1] !== 'go' && $segments[1] !== 'mobile' && $segments[1] !== 'ok' && $segments[1] !== 'ok_mobile') 
		{
			array_splice($segments, 1, 0, 'index');
		}
		if (isset($segments[0]) && $segments[0] == 'edit' && 
			isset($segments[1]) && 
				$segments[1] !== 'index' && $segments[1] !== 'form' && $segments[1] !== 'post' && 
				$segments[1] !== 'delete' && $segments[1] !== 'delete_confirm') 
		{
			array_splice($segments, 1, 0, 'form');
		}
		if (isset($segments[0]) && $segments[0] == 'submit' && 
			isset($segments[1]) && $segments[1] !== 'post' && $segments[1] !== 'form') 
		{
			array_splice($segments, 1, 0, 'form');
		}
	}

	function _field_order_check($key, $val)
	{
		$retrun_val = NULL;

		switch ($key) {
			case 'f':
				if (in_array(strtolower($val), array('title', 'date', 'rating', 'hits', 'access'))) {
					$retrun_val = $val;
				}
				break;
			case 'ord':
				if (in_array(strtolower($val), array('asc', 'desc'))) {
					$retrun_val = $val;
				}
				break;
			default:
				break;
		}

		return $retrun_val;
	}

	function _value_check($key, $arg_k, $default = NULL)
	{
		$val   = $default;
		$value = $default;

		if ($this->get_query == TRUE) {
			if (isset($_GET[$key]) && (!empty($_GET[$key]) || $_GET[$key] == 0)) {
				$val = $_GET[$key];
			}
		} else {
			if (! empty($this->args[$arg_k]) || @$this->args[$arg_k] === '0') {
				$val = $this->args[$arg_k];
			}
		}

		if ($val != NULL) {
			if ($key == 'f' || $key == 'ord') {
				$value = $this->_field_order_check($key, $val);
			} else {
				$value = intval($val);
			}
		}
		
		return $value;
	}

	function _check_args(/*$get_query = FALSE*/)
	{
	//	$this->get_query = $get_query;

		$args = array();

		switch ($this->class) {
			case 'index':
				if ($this->method == 'item') {
					$args[0] = $this->_value_check('lid', 0);
				}
				if ($this->method == 'index' || $this->method == 'items') {
					$args[0] = $this->_value_check('f', 0);
					$args[1] = $this->_value_check('ord', 1);
					$args[2] = $this->_value_check('p', 2);
				}
				if ($this->method == 'user') {
					$args[0] = $this->_value_check('uid', 0);
					$args[1] = $this->_value_check('f', 1);
					$args[2] = $this->_value_check('ord', 2);
					$args[3] = $this->_value_check('p', 3);
				}
				break;
			case 'category':
				if ($this->method == 'index') {
					$args[0] = $this->_value_check('cid', 0);
					$args[1] = $this->_value_check('f', 1);
					$args[2] = $this->_value_check('ord', 2);
					$args[3] = $this->_value_check('p', 3);
				}
				break;
			case 'adult':
			case 'future':
				if ($this->method == 'index') {
					$args[0] = $this->_value_check('p', 0);
				}
				break;
			case 'edit':
			case 'broken':
			case 'rating':
			case 'request':
				if ($this->method == 'index' || $this->method == 'form') {
					$args[0] = $this->_value_check('lid', 0);
				}
				break;
			case 'frame_header':
				if ($this->method == 'index') {
					$args[0] = $this->_value_check('lid', 0);
				}
				break;
			case 'ranking':
				if ($this->method == 'index' || $this->method == 'hits' || $this->method == 'rating' || $this->method == 'access') {
					$args[0] = $this->_value_check('cid', 0, 0);
					$args[1] = $this->_value_check('pp', 1, 10);
				}
				break;
			case 'rss':
				if ($this->method == 'index' || $this->method == 'category') {
					$args[0] = $this->_value_check('cid', 0);
				}
				break;
			case 'submit':
				if ($this->method == 'index' || $this->method == 'form') {
					$args[0] = $this->_value_check('cid', 0);
				}
				break;
			case 'visit':
				if ($this->method == 'index' || $this->method == 'go' || $this->method == 'mobile' || $this->method == 'ok' || $this->method == 'ok_mobile') {
					$args[0] = $this->_value_check('lid', 0);
				}
				break;
			default:
				break;
		}

		foreach ($args as $k => $arg) {
			if ($arg === NULL) {
				unset($args[$k]);
			}
		}

		$this->args = $args;
	}


}
